package com.sky.controller.user;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.impl.Userservicelml;
import com.sky.utils.JwtUtil;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.UserLoginVO;
import io.jsonwebtoken.Jwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private Userservicelml userservicelml;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/user/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO)//前端发送code
    {
        //调用service 返回openid 使用Userbean封装
        User user = userservicelml.wxlogin(userLoginDTO);
        log.info("登录的code{}", userLoginDTO.getCode());
        HashMap<String, Object> map = new HashMap<>();
        map.put(JwtClaimsConstant.USER_ID, user.getId());
        //生成Jwt令牌
        String jwt = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), map);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(jwt)
                .build();
        log.info(" UserLoginVO is {}", userLoginVO);
        return Result.success(userLoginVO);

    }


}
